package kafka;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

/**
 * kafka 生产者
 */
public class Producer {

    public static void main(String[] args) {
        // 创建配置对象
        Properties prop = new Properties();
        // kafka服务器地址
        prop.put("bootstrap.servers","master:9092,slave1:9092,slave2:9092");
        // 设置序列化
        prop.put("key.serializer", StringSerializer.class);
        prop.put("value.serializer",StringSerializer.class);
        // 创建生产者
        KafkaProducer<String,String> producer = new KafkaProducer<>(prop);
        // 封装消息
        ProducerRecord<String,String> record0 = new ProducerRecord<String, String>("wc",0,"hadoop,flink,kafka","hadoop,flink,kafka");
        ProducerRecord<String,String> record1 = new ProducerRecord<String, String>("wc",1,"hadoop,flink,kafka","hadoop,flink,kafka");
        ProducerRecord<String,String> record2 = new ProducerRecord<String, String>("wc",2,"hadoop,flink,kafka","hadoop,flink,kafka");

        producer.send(record0);
        producer.send(record1);
        producer.send(record2);
        producer.close();
    }
}
